<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Java 包装类值比较最佳实践</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 2px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .code-block {
            background: #1e1e1e;
            color: #d4d4d4;
            border-radius: 12px;
            position: relative;
            overflow: hidden;
        }
        
        .code-block::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 40px;
            background: #2d2d2d;
            border-bottom: 1px solid #3e3e3e;
        }
        
        .code-dots {
            position: absolute;
            top: 12px;
            left: 20px;
            display: flex;
            gap: 8px;
        }
        
        .dot {
            width: 12px;
            height: 12px;
            border-radius: 50%;
        }
        
        .dot-red { background: #ff5f56; }
        .dot-yellow { background: #ffbd2e; }
        .dot-green { background: #27c93f; }
        
        .highlight-box {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            font-weight: 700;
        }
        
        .section-divider {
            height: 2px;
            background: linear-gradient(to right, transparent, #667eea, transparent);
            margin: 3rem 0;
        }
        
        @keyframes float {
            0% { transform: translateY(0px); }
            50% { transform: translateY(-10px); }
            100% { transform: translateY(0px); }
        }
        
        .float-animation {
            animation: float 3s ease-in-out infinite;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto text-center">
            <div class="float-animation inline-block mb-6">
                <i class="fas fa-code text-6xl opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6" style="font-family: 'Noto Serif SC', serif;">
                Java 包装类值比较的艺术
            </h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto leading-relaxed">
                深入理解 <code class="bg-white bg-opacity-20 px-3 py-1 rounded">==</code> 与 <code class="bg-white bg-opacity-20 px-3 py-1 rounded">equals()</code> 的本质区别
            </p>
        </div>
    </div>

    <!-- Main Content -->
    <div class="max-w-6xl mx-auto px-6 py-12">
        <!-- 核心原则卡片 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12 card-hover">
            <div class="flex items-center mb-6">
                <i class="fas fa-lightbulb text-4xl text-yellow-500 mr-4"></i>
                <h2 class="text-3xl font-bold">核心原则</h2>
            </div>
            <div class="bg-gradient-to-r from-purple-50 to-pink-50 rounded-xl p-6">
                <p class="text-xl leading-relaxed">
                    在 Java 中，包装类的值比较应使用 <code class="bg-gray-800 text-white px-3 py-1 rounded font-mono">equals()</code> 方法，
                    而不是使用 <code class="bg-gray-800 text-white px-3 py-1 rounded font-mono">==</code> 操作符。
                </p>
            </div>
        </div>

        <!-- 概念关系图 -->
        <div class="bg-white rounded-2xl shadow-xl p-8 mb-12">
            <h2 class="text-3xl font-bold mb-6 text-center">
                <i class="fas fa-project-diagram mr-3 text-purple-600"></i>
                概念关系图
            </h2>
            <div class="mermaid">
                graph TD
                    A[Java 值比较] --> B[基本类型]
                    A --> C[引用类型]
                    B --> D[使用 == 比较值]
                    C --> E[包装类]
                    C --> F[其他对象]
                    E --> G[== 比较引用]
                    E --> H[equals() 比较值]
                    F --> I[== 比较引用]
                    F --> J[equals() 比较内容]
                    
                    style A fill:#667eea,stroke:#fff,stroke-width:3px,color:#fff
                    style E fill:#f093fb,stroke:#fff,stroke-width:3px,color:#fff
                    style H fill:#27c93f,stroke:#fff,stroke-width:3px,color:#fff
            </div>
        </div>

        <!-- 详细说明部分 -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <!-- == 操作符说明 -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="bg-red-100 p-4 rounded-full mr-4">
                        <i class="fas fa-equals text-2xl text-red-600"></i>
                    </div>
                    <h3 class="text-2xl font-bold">== 操作符的作用</h3>
                </div>
                
                <div class="space-y-4">
                    <div class="bg-gray-50 rounded-lg p-4 border-l-4 border-red-500">
                        <h4 class="font-bold text-lg mb-2 flex items-center">
                            <i class="fas fa-cube mr-2 text-red-500"></i>
                            对于基本类型
                        </h4>
                        <p class="text-gray-700">
                            <code class="bg-gray-200 px-2 py-1 rounded">==</code> 用于比较两个基本类型的值是否相等。
                        </p>
                    </div>
                    
                    <div class="bg-gray-50 rounded-lg p-4 border-l-4 border-red-500">
                        <h4 class="font-bold text-lg mb-2 flex items-center">
                            <i class="fas fa-object-group mr-2 text-red-500"></i>
                            对于对象类型
                        </h4>
                        <p class="text-gray-700">
                            <code class="bg-gray-200 px-2 py-1 rounded">==</code> 用于比较两个对象的引用是否指向同一个对象。
                        </p>
                    </div>
                </div>
            </div>

            <!-- equals() 方法说明 -->
            <div class="bg-white rounded-2xl shadow-xl p-8 card-hover">
                <div class="flex items-center mb-6">
                    <div class="bg-green-100 p-4 rounded-full mr-4">
                        <i class="fas fa-check-double text-2xl text-green-600"></i>
                    </div>
                    <h3 class="text-2xl font-bold">equals() 方法的作用</h3>
                </div>
                
                <div class="bg-gray-50 rounded-lg p-4">
                    <p class="text-gray-700 leading-relaxed mb-3">
                        <code class="bg-gray-200 px-2 py-1 rounded">equals()</code> 方法是 <code class="bg-gray-200 px-2 py-1 rounded">Object</code> 类中的一个方法，它用于比较两个对象的内容是否相等。
                    </p>
                    <p class="text-gray-700 leading-relaxed">
                        包装类重写了 <code class="bg-gray-200 px-2 py-1 rounded">equals()</code> 方法，以确保两个包装类对象的值相等时，
                        <code class="bg-gray-200 px-2 py-1 rounded">equals()</code> 方法返回 <code class="bg-green-200 text-green-800 px-2 py-1 rounded">true</code>。
                    </p>
                </div>
            </div>
        </div>

        <!-- 为什么使用 equals() -->
        <div class="bg-gradient-to-r from-purple-600 to-pink-600 rounded-2xl shadow-xl p-8 mb-12 text-white">
            <h3 class="text-3xl font-bold mb-8 text-center">
                <i class="fas fa-question-circle mr-3"></i>
                为什么使用 equals() 方法？
            </h3>
            
            <div class="grid md:grid-cols-2 gap-6">
                <div class="bg-white bg-opacity-10 backdrop-blur rounded-xl p-6 border border-white border-opacity-20">
                    <div class="flex items-start">
                        <i class="fas fa-shield-alt text-3xl mr-4 mt-1"></i>
                        <div>
                            <h4 class="text-xl font-bold mb-3">避免误判</h4>
                            <p class="opacity-90 leading-relaxed">
                                对于包装类，特别是在范围之外的对象（如 <code class="bg-white bg-opacity-20 px-2 py-1 rounded">Integer</code> 超过 -128 到 127 的范围），
                                <code class="bg-white bg-opacity-20 px-2 py-1 rounded">==</code> 比较的只是引用而非值。
                                因此，可能会出现相同值的对象通过 <code class="bg-white bg-opacity-20 px-2 py-1 rounded">==</code> 返回 
                                <code class="bg-red-200 text-red-800 px-2 py-1 rounded">false</code> 的情况。
                            </p>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white bg-opacity-10 backdrop-blur rounded-xl p-6 border border-white border-opacity-20">
                    <div class="flex items-start">
                        <i class="fas fa-sync-alt text-3xl mr-4 mt-1"></i>
                        <div>
                            <h4 class="text-xl font-bold mb-3">一致性</h4>
                            <p class="opacity-90 leading-relaxed">
                                <code class="bg-white bg-opacity-20 px-2 py-1 rounded">equals()</code> 方法在包装类中被重写，
                                以确保对象值的比较是一致的。
                            </p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 示例代码 -->
        <div class="bg-white rounded-2xl shadow-xl overflow-hidden mb-12">
            <div class="bg-gradient-to-r from-gray-800 to-gray-900 text-white p-6">
                <h3 class="text-2xl font-bold flex items-center">
                    <i class="fas fa-code mr-3"></i>
                    示例代码
                </h3>
            </div>
            
            <div class="code-block p-8 pt-16 relative">
                <div class="code-dots">
                    <div class="dot dot-red"></div>
                    <div class="dot dot-yellow"></div>
                    <div class="dot dot-green"></div>
                </div>
                
                <pre class="text-lg leading-relaxed"><code><span style="color: #569cd6;">Integer</span> <span style="color: #9cdcfe;">a</span> = <span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">Integer</span>(<span style="color: #b5cea8;">128</span>);
<span style="color: #569cd6;">Integer</span> <span style="color: #9cdcfe;">b</span> = <span style="color: #569cd6;">new</span> <span style="color: #4ec9b0;">Integer</span>(<span style="color: #b5cea8;">128</span>);

<span style="color: #4ec9b0;">System</span>.<span style="color: #9cdcfe;">out</span>.<span style="color: #dcdcaa;">println</span>(<span style="color: #9cdcfe;">a</span> == <span style="color: #9cdcfe;">b</span>); <span style="color: #6a9955;">// 输出 false，因为 a 和 b 引用不同的对象</span>